package com.haochang.chunkvod.model.order;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.haochang.base.BaseActivity;
import com.haochang.chunkvod.app.config.ApiConfig;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpMethodEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpRequestLoadingEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestBuilder;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestTask;

/**
 * @author qintao
 *         created at 2016/6/25 15:12
 *         微信确认点餐数据处理
 */

public class WeChatOrderData
{

	private BaseActivity context;

	public WeChatOrderData(BaseActivity context)
	{
		this.context = context;
	}

	public interface IWeChatOrderListListener
	{
		public void onRequestSuccess(List<WeChatInfo> list);
	}

	public interface IWeChatOrderDetailListener
	{
		public void onRequestSuccess(int cashTotal, List<WeChatInfo> list);
	}

	public interface IWeChatOrderInfoListener
	{
		public void onRequestSuccess(JSONObject jsonObject);
	}

	/**
	 * 微信点餐确认列表查询
	 *
	 * @param listener 网络请求监听
	 */
	public void requestWeChatOrderList(final IWeChatOrderListListener listener)
	{
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.AFFIRMANCE_LIST).param(null)
				.httpMethodEnum(HttpMethodEnum.GET).httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT)
				.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (jsonObject != null)
						{
							List<WeChatInfo> list = parseWeChatInfoListJson(jsonObject);
							if (listener != null)
							{
								listener.onRequestSuccess(list);
							}
						}
					}
				}).build();
		httpRequestTask.execute();
	}

	/**
	 * 微信点餐确认列表查询
	 * @param billId 账单id
	 * @param listener 网络请求监听
	 */
	public void requestWeChatOrderDetail(String billId, final IWeChatOrderDetailListener listener)
	{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("bill_id", billId);
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.AFFIRMANCE_DETAIL).param(map)
				.httpMethodEnum(HttpMethodEnum.GET).httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT)
				.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (jsonObject != null)
						{
							JSONObject obj = jsonObject.optJSONObject("result");
							List<WeChatInfo> list = parseWeChatDetailInfoListJson(obj);
							int cashTotal = obj.optInt("cash_total");
							if (listener != null)
							{
								listener.onRequestSuccess(cashTotal, list);
							}
						}
					}
				}).build();
		httpRequestTask.execute();
	}

	/**
	 * 微信点餐取消
	 * @param billId 账单id
	 * @param listener 网络请求监听
	 */
	public void requestWeChatOrderCancel(String billId, final IWeChatOrderInfoListener listener)
	{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("bill_id", billId);
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.AFFIRMANCE_DETAIL).param(map)
				.httpMethodEnum(HttpMethodEnum.DELETE).httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT)
				.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (listener != null)
						{
							listener.onRequestSuccess(jsonObject);
						}
					}
				}).build();
		httpRequestTask.execute();
	}

	/**
	 * 微信点餐确定
	 * @param billId 账单id
	 * @param jsonArray 确认数据  
	 * @param listener 网络请求监听
	 */
	public void requestWeChatOrderSure(String billId, JSONArray jsonArray, final IWeChatOrderInfoListener listener)
	{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("bill_id", billId);
		map.put("orders", jsonArray.toString());
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.AFFIRMANCE_DETAIL).param(map)
				.httpMethodEnum(HttpMethodEnum.POST).httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT)
				.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (listener != null)
						{
							listener.onRequestSuccess(jsonObject);
						}
					}
				}).build();
		httpRequestTask.execute();
	}

	/**
	 * 解析微信点餐列表信息
	 *
	 * @param jsonObject jsonObject
	 * @return 微信点餐列表
	 */
	private List<WeChatInfo> parseWeChatInfoListJson(JSONObject jsonObject)
	{
		ArrayList<WeChatInfo> infoList = new ArrayList<WeChatInfo>();
		JSONArray goodsArray = jsonObject.optJSONArray("result");
		if (goodsArray != null && goodsArray.length() > 0)
		{
			for (int i = 0; i < goodsArray.length(); i++)
			{
				JSONObject jObject = goodsArray.optJSONObject(i);
				if (jObject == null)
					continue;
				WeChatInfo info = new WeChatInfo();
				info.setBillId(jObject.optString("bill_id"));
				info.setOrderNum(jObject.optInt("order_quantity"));
				info.setRoomNum(jObject.optString("room_code"));
				info.setPayTime(jObject.optString("time"));
				infoList.add(info);
			}
		}
		return infoList;
	}

	/**
	 * 解析微信点餐详细列表信息
	 *
	 * @param jsonObject jsonObject
	 * @return  微信点餐详情列表
	 */
	private List<WeChatInfo> parseWeChatDetailInfoListJson(JSONObject jsonObject)
	{
		ArrayList<WeChatInfo> infoList = new ArrayList<WeChatInfo>();
		JSONArray weChatArray = jsonObject.optJSONArray("orders");
		if (weChatArray != null && weChatArray.length() > 0)
		{
			for (int i = 0; i < weChatArray.length(); i++)
			{
				JSONObject jObject = weChatArray.optJSONObject(i);
				WeChatInfo info = new WeChatInfo();
				info.setOrderId(jObject.optString("order_id"));
				info.setOrderUserId(jObject.optString("order_user_id"));
				info.setOrderTime(jObject.optString("order_time"));
				info.setNickName(jObject.optString("nickname"));
				info.setCashTotal(jObject.optInt("cash_total"));
				JSONArray goodsArray = jObject.optJSONArray("foods");
				if (goodsArray != null && goodsArray.length() > 0)
				{
					ArrayList<GoodsInfo> goodsInfoList = new ArrayList<GoodsInfo>();
					for (int j = 0; j < goodsArray.length(); j++)
					{
						JSONObject object = goodsArray.optJSONObject(j);
						GoodsInfo goodsInfo = new GoodsInfo();
						goodsInfo.setGoodsId(object.optString("id"));
						goodsInfo.setGoodsName(object.optString("name"));
						goodsInfo.setOrderNum(object.optInt("quantity"));
						goodsInfo.setSurplus(object.optInt("rest_quantity"));
						goodsInfo.setUnit(object.optString("unit"));
						goodsInfo.setPrice(object.optInt("price"));
						String type = object.optString("type");
						if (!type.equals("goods"))
							goodsInfo.setGoods_type(1);
						goodsInfo.setCashTotal(object.optInt("cash_total"));
						JSONObject testes = object.optJSONObject("taste");
						if (null != testes)
						{
							JSONArray flavor = testes.optJSONArray("tastes");
							if (flavor != null && flavor.length() > 0)
								goodsInfo.setFlavor(flavor.toString());
							JSONArray tests = testes.optJSONArray("list");
							if (tests != null && tests.length() > 0)
								goodsInfo.setTaste_types(tests.toString());
						}
						JSONArray retailsArray = object.optJSONArray("goods");
						if (retailsArray != null && retailsArray.length() > 0)
						{
							List<FoodDetailInfo> retails = new ArrayList<FoodDetailInfo>();
							for (int k = 0; k < retailsArray.length(); k++)
							{
								FoodDetailInfo foodDetailInfo = new FoodDetailInfo();
								JSONObject obj = retailsArray.optJSONObject(k);
								foodDetailInfo.setFood_id(obj.optString("goods_id"));
								foodDetailInfo.setName(obj.optString("name"));
								foodDetailInfo.setQuantity(obj.optInt("quantity"));
								foodDetailInfo.setFlavor(obj.optString("tastes"));
								foodDetailInfo.setUnit(obj.optString("unit"));
								retails.add(foodDetailInfo);
							}
							goodsInfo.setFoodDetailInfoList(retails);
						}
						goodsInfoList.add(goodsInfo);
					}
					info.setGoodsInfoList(goodsInfoList);
				}
				infoList.add(info);
			}
		}
		return infoList;
	}

}
